package com.example.toutiao.view

import android.content.Context
import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import androidx.core.content.ContextCompat
import com.example.toutiao.R
import kotlinx.android.synthetic.main.my_checkbox.view.*

class MyCheckBox@JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = R.attr.editTextStyle
): FrameLayout(context,attrs,defStyleAttr) {
    private var isChecked=false
    private var isSelected=R.mipmap.icon_checked
    private var isUnSelected=R.mipmap.icon_unchecked
    private var mClickableSpanText:ClickableSpanText?=null

    init {
        View.inflate(context,R.layout.my_checkbox,this)
//        initSpan()
    }

    /**
     * 该方法用于截取资源文件，并对资源文件进行截取
     * 对截取的字符串进行 颜色修改和事件添加
     */
    fun initSpan(agreementIds:Int){
        val agreementStr=resources.getString(agreementIds)
        val span=SpannableString(agreementStr)
        span.setSpan(object:ClickableSpan(){
            override fun onClick(widget: View) {
                mClickableSpanText?.onClick(1)
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color=ContextCompat.getColor(context,R.color.c_222222)
                ds.isUnderlineText=false
            }

        },7,12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        span.setSpan(object:ClickableSpan(){
            override fun onClick(widget: View) {
                mClickableSpanText?.onClick(2)
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color=ContextCompat.getColor(context,R.color.c_222222)
                ds.isUnderlineText=false
            }

        },15,20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        tv_text.movementMethod=LinkMovementMethod.getInstance()
        tv_text.highlightColor= Color.TRANSPARENT
        tv_text.text=span
    }
    fun changeState(){
        isChecked=!isChecked
        if(isChecked){
            image.setImageResource(isSelected)
        }else{
            image.setImageResource(isUnSelected)
        }
    }
    fun isShow(isShow:Boolean){
        if(isShow){
            image.visibility=View.VISIBLE
        }else{
            image.visibility=View.GONE
        }
    }
    fun isChecked():Boolean=isChecked
    fun setChecked(isChecked:Boolean){
        this.isChecked=isChecked
        if(isChecked){
            image.setImageResource(isSelected)
        }else{
            image.setImageResource(isUnSelected)
        }
    }
    interface ClickableSpanText{
        fun onClick(type:Int)
    }

    fun setClickableSpanText(mClickableSpanText:ClickableSpanText){
        this.mClickableSpanText=mClickableSpanText

    }
}